import scipy as sp
import numpy as np

def serialize_crs_matrix(csr_matrix):
    serialized_matrix = {
        'meta':'csr',
        'data':csr_matrix.data.tolist(),
        'indices':csr_matrix.indices.tolist(),
        'indptr':csr_matrix.indptr.tolist(),
        '_shape':csr_matrix.shape,
    }
    return serialized_matrix

def deserialize_crs_matrix(crs_dict,data_type=np.float64, indices_type=np.int32,indptr_type=np.int32):
    crs_matrix = sp.sparse.csr_matrix(tuple(crs_dict['_shape']))
    crs_matrix.data = np.array(crs_dict['data']).astype(data_type)
    crs_matrix.indices = np.array(crs_dict['indices']).astype(indices_type)
    crs_matrix.indptr = np.array(crs_dict['indptr']).astype(indptr_type)
    return crs_matrix

